<?php

namespace App\Http\Controllers\Api;

use App\Model\FormRecord;
use App\Model\Task;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Article;
use App\Model\ArticleCategories;
use App\User;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;

class ArticleController extends Controller
{
    //
    public function index(){
        $page_size=10;
        $page=1;
        $categoryId=1;
        $model=Article::select(['article_id','article_id as id','title','serial_number','introduction','like_count', 'view_count', 'comment_count', 'is_visible_all',
            'created_at', 'updated_at', 'published_at', 'approved_at',  'is_draft', 'is_published', 'is_approved', 'is_progressive_approval', 'step', 'status',
            'article_category_id','cover_photo_id','order',
            'author_id','created_by','updated_by','approved_by']);
      $model->with(['articleCategories']);
      $model->list($categoryId);
      $paginator=$model->paginate($page_size,['*'],'page',$page);

        return response()->json($paginator,200,['errcode'=>0,'errmsg'=>'文章列表'],JSON_NUMERIC_CHECK);
    }
    //获取新闻类别列表
    public function cateList(){
        $builder=ArticleCategories::select(['*']);
        echo $builder->with(['articles'])->get();
        die;
        return response()->json($builder,200,[],JSON_NUMERIC_CHECK);
    }
    //任务多态测试
    public function taskList(){
        DB::connection()->enableQueryLog();
        DB::getQueryLog();
        $page_size=10;
        $page=1;
//        $builder=FormRecord::select(['*'])->limit(10);
//     $result=$builder->with(['superPeople2'])->get();
        $builder=User::select(['*']);
        $result=$builder->with(['photos'=>function($q){
            $q->select(['photos.photo_id','photos.photo_name']);
        }])->paginate($page_size,['*'],'page',$page);
//       var_dump(DB::getQueryLog());
//        die;
        return response()->json($result,200,[],JSON_NUMERIC_CHECK);
    }

}
